﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Controls.General.Popup;
using JBeamLibrary;
using JBeamLibrary.VehicleParts;
using JBeamLibrary.VehicleParts.Elements;

namespace DriveBeamEditor.Controls.BeamNG.Popups
{
    public partial class SlotSelectionPopup : PopupContainer
    {
        #region Fields

        private Vehicle _vehicle;

        public event EventHandler VehicleChanged;
        public event EventHandler<Slot> SlotSelected;

        #endregion

        #region Properties

        public Vehicle Vehicle
        {
            get { return _vehicle; }
            set
            {
                if (_vehicle == value) return;
                _vehicle = value;
                OnVehicleChanged();
            }
        }

        public Slot SelectedSlot
        {
            get { return slotSelectionControl.SelectedSlot; }
        }

        #endregion

        #region CTORs

        public SlotSelectionPopup()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        protected override void OnVisibleChanged(EventArgs e)
        {
            if (Visible)
                slotSelectionControl.Reload();
            base.OnVisibleChanged(e);
        }

        private void xbSelect_Click(object sender, EventArgs e)
        {
            if (SlotSelected != null)
                SlotSelected(this, SelectedSlot);
            Close();
        }

        private void xbRefresh_Click(object sender, EventArgs e)
        {
            slotSelectionControl.Reload();
        }

        private void slotSelectionControl_SlotValidated(object sender, Slot e)
        {
            if (SlotSelected != null)
                SlotSelected(this, e);
            Close();
        }

        #endregion

        #region Methods

        protected virtual void BindVehicle()
        {
            slotSelectionControl.Vehicle = Vehicle;
        }

        protected virtual void OnVehicleChanged()
        {
            BindVehicle();

            if (VehicleChanged != null)
                VehicleChanged(this, new EventArgs());
        }

        #endregion
    }
}
